/*
 * InformesDAO.java
 *
 * Created on 31 de octubre de 2006, 04:29 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.src.computec.radar.services.utilities.dao;

import com.src.computec.radar.base.dao.GenericDAO;
import com.src.computec.radar.base.dao.exception.DAOException;
import com.src.computec.radar.services.base.constantes.ConstantesGenerales;
import com.src.computec.radar.services.utilities.dto.FileManagerDto;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 *
 * @author aocampo
 */
public class FileManagerDAO extends GenericDAO implements BaseFileManagerDAO 
{
    private String SCHEMA;
    private String ACTIVE_STATE;
    private String DOWNLOADED_STATE;
    
    /** Creates a new instance of LdapDAO */
    public FileManagerDAO() 
    {
        ConstantesGenerales constantesGenerales = ConstantesGenerales.getInstance();
        this.SCHEMA = constantesGenerales.SCHEMA;
        this.ACTIVE_STATE = constantesGenerales.ACTIVE_STATE;
        this.DOWNLOADED_STATE = constantesGenerales.DOWNLOADED_STATE;
    }
    
    public void insertFileManager(FileManagerDto dto) throws DAOException 
    {
        Connection con = null;
        PreparedStatement statement = null;
        try {
            con = getConnectionDB2();
            statement = con.prepareStatement("INSERT INTO " + SCHEMA + ".FILE_MANAGER (ID_DESCARGA,CODIGO_RESPONSABLE,ORIGEN,RUTA_ARCHIVO,ESTADO) " +
                                             "VALUES (?, ?, ?, ?, ?)");
            int indice = 0;
            statement.setTimestamp(++indice, dto.getIdDescarga());
            statement.setObject(++indice, dto.getCodigoResponsable());
            statement.setObject(++indice, dto.getOrigen());
            statement.setObject(++indice, dto.getRutaArchivo());
            statement.setObject(++indice, dto.getEstado());
            statement.executeUpdate();
        }
        catch (SQLException e) {
            throw new DAOException("FileManagerDAO:insertFileManager: " + e.toString(), e);
        }
        finally {
            closeConnection(con, statement);
        }
    }
    
    
     public void insertFileManagerKey(FileManagerDto dto) throws DAOException 
    {
        Connection con = null;
        PreparedStatement statement = null;
        try {
            con = getConnectionDB2();
            statement = con.prepareStatement("INSERT INTO " + SCHEMA + ".FILE_MANAGER (ID_DESCARGA,CODIGO_RESPONSABLE,ORIGEN,RUTA_ARCHIVO,ESTADO,LLAVE) " +
                                             "VALUES (?, ?, ?, ?, ?,?)");
            int indice = 0;
            statement.setTimestamp(++indice, dto.getIdDescarga());
            statement.setObject(++indice, dto.getCodigoResponsable());
            statement.setObject(++indice, dto.getOrigen());
            statement.setObject(++indice, dto.getRutaArchivo());
            statement.setObject(++indice, dto.getEstado());
	    statement.setObject(++indice, new Integer(dto.getKey()));
            statement.executeUpdate();
        }
        catch (SQLException e) {
            throw new DAOException("FileManagerDAO:insertFileManagerKey: " + e.toString(), e);
        }
        finally {
            closeConnection(con, statement);
        }
    }
     
    public String getPathFileDownload(FileManagerDto dto) throws DAOException
    {
        String pathRetorno = null;
        Connection con = null;
        PreparedStatement statement = null;
        PreparedStatement statement2 = null;
        
        ResultSet rs = null;
        try {
            con = getConnectionDB2();
            statement  = con.prepareStatement("SELECT RUTA_ARCHIVO FROM " + SCHEMA + ".FILE_MANAGER WHERE ID_DESCARGA = ? AND ESTADO = "+ ACTIVE_STATE +" AND RUTA_ARCHIVO IS NOT NULL ");
            statement2 = con.prepareStatement("UPDATE " + SCHEMA + ".FILE_MANAGER SET ESTADO = "+ DOWNLOADED_STATE +" WHERE ID_DESCARGA = ? ");
            
            int indice = 0;
            statement.setTimestamp(++indice, dto.getIdDescarga());
            rs = statement.executeQuery();
            
            if(rs.next())
            {
                int indice1 = 0;                
                pathRetorno = rs.getString(++indice1);
                indice1 = 0;                
                statement2.setTimestamp(++indice1, dto.getIdDescarga());
                statement2.executeUpdate();   
                con.commit();
            }
        }
        catch (SQLException e) {
            throw new DAOException("FileManagerDAO:getPathFileDownload: " + e.toString(), e);
        }
        finally 
        {
            closeConnection(null, statement2);
            closeConnection(con,statement, rs);                       
        }
        return pathRetorno;
    }
}